package book;

public class BookList {
    private Book[] books = new Book[10];
    private int usedSize;
    public BookList() {
        books[0] = new Book("水浒传", "罗贯中", 20, "小说");
        books[1] = new Book("红楼梦", "曹雪芹", 40, "小说");
        books[2] = new Book("十万个为什么", "张大量", 30, "知识");
        usedSize = 3;
    }

    public boolean isFull() {
        return books.length == usedSize;
    }

    public int findIndexOfName(String name) {
        Book tmp = new Book(name, null, 0, null);
        for (int i = 0; i < usedSize; i++) {
            if(tmp.equals(books[i])) {
                return i;
            }
        }
        return -1;
    }

    public Book findBookOfName(String name) {
        int index = findIndexOfName(name);
        return index == -1 ? null : books[index];
    }

    public Book delBookOfName(String name) {
        int index = findIndexOfName(name);
        if(index == -1) return null;
        Book tmp = books[index];
        for (int i = index; i < usedSize - 1; i++) {
            books[i] = books[i + 1];
        }
        usedSize--;
        return tmp;
    }

    public void showAllBook() {
        for (int i = 0; i < usedSize; i++) {
            System.out.println(books[i]);
        }
    }

    public boolean addBook(Book book) {
        if(isFull()) return false;
        books[usedSize++] = book;
        return true;
    }

    public int getUsedSize() {
        return usedSize;
    }
}
